package com.zczy.controller;

import com.zczy.entity.ZczyItem;
import com.zczy.mapper.ZczyItemMapper;
import com.zczy.service.FileService;
import com.zczy.service.ZczyItemService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.UUID;

@Controller
//@RequestMapping("showOff")
public class ShowController {
    @Autowired
    private ZczyItemService zczyItemService;

    @Autowired
    private ZczyItemMapper zczyItemMapper;

    @Autowired
    private FileService fileService;

    @GetMapping("index")
    private String index(Model model){
        Integer count = zczyItemService.lambdaQuery().count();

        model.addAttribute("integer",count);
        return "index";
    }


    @GetMapping("/productShow/{id}")
    public String productShow(@PathVariable("id")Integer id, Model model){
        ZczyItem byId = zczyItemService.getById(id);


        model.addAttribute("byId",byId);
        BigDecimal targetAmount = byId.getTargetAmount();//目标金额
        BigDecimal perMoney = byId.getPerMoney();//起投金额
        BigDecimal actAmount = byId.getActAmount();//已投金额
        //subtract减法；divide除法
        BigDecimal m =targetAmount.subtract(actAmount);
        BigDecimal divide = m.divide(perMoney);
        double v = divide.doubleValue();//Big Decimal转Double
        double result = Math.floor(v);//需要向下取整
        int r = (int) result;//取整
        model.addAttribute("result",r);

        //求百分比
        double v2 = actAmount.doubleValue();
        double v3 = targetAmount.doubleValue();
        double v4 = v2 / v3;//已投金额除于目标金额
        double d = v4 * 100;
        int floor = (int)d;
        model.addAttribute("floor",floor);
        String proportion = "width:" + floor + "%";
        model.addAttribute("proportion",proportion);

        return "展示类项目";
    }


    @GetMapping("productParticulars")
    public String productParticulars(){
        return "展示类详情";
    }

    @GetMapping("tradingProject")
    public String tradingProject(){
        return "交易类项目";
    }

    @GetMapping("fundingCommunity")
    public String fundingCommunity(){
        return "众筹社区";
    }

    @GetMapping("okProject")
    public String okProject(){
        return "已成功项目";
    }

    @GetMapping("/sponsorFunding")
    public String sponsorFunding(HttpSession session ,Model model){
//        session.getAttribute()
//        //判断是否登入
//        if (){
//            return "登录";
//        }else if (){
//         //判断是否实名认证
//            return "实名认证";
//        }else {

//        List<ZczyItem> type = zczyItemService.lambdaQuery().select(ZczyItem::getType).list();//查询所有Type字段的值
//        model.addAttribute("type",type);



        return "发布项目-第一步";
//        }


    }

    //查看静态页面
    @GetMapping("sponsorFunding2")
    public String z2(){
        return "发布项目-第二步";
    }

    @GetMapping("sponsorFunding3")
    public String z3(){
        return "发布项目-第三步";
    }


    @ApiOperation("证件上传")
    @PostMapping("/upload")
    public String upload(@ApiParam(value = "上传的文件", required = true) @RequestParam("file") MultipartFile file) throws Exception {
        //原始文件名称
        String originalFilename = file.getOriginalFilename();
        //文件后缀
        String suffix = originalFilename.substring(originalFilename.lastIndexOf(".")-1);
        //文件名称
        String fileName = UUID.randomUUID().toString()+suffix;
        //上传文件，返回文件下载url
        String fileurl = fileService.upload(file.getBytes(), fileName);
        return fileurl;

    }

}
